

* dummies for present on first of each quarter
foreach y in  2007 2008 2009 2010  {
foreach x in apr oct  {
gen `x'`y'=0
replace `x'`y'=1 if rec_in<=td(01`x'`y') & rec_out>td(01`x'`y')
}
}

* interaction terms between present on the first of each quarter and facility FE
* the relevant date FE's go back 8 months before entry
* analysis sample includes those released after April 2008 and before October 2010
tab labelfac61 if RELEASE_YR!=607 , gen(lab_) 
foreach x in  oct2007 apr2008 oct2008 apr2009 oct2009 apr2010 oct2010{
foreach y in  lab_1 lab_2 lab_3 lab_4 lab_5 lab_6 lab_7 lab_8 lab_9 lab_10 lab_11 lab_12 lab_13 lab_14 lab_15 lab_16 lab_17 lab_18 lab_19 lab_20 lab_21 lab_22 lab_23 lab_24 lab_25 lab_26 lab_27 lab_28 lab_29 lab_30 lab_31 lab_32 lab_33 {
gen `x'`y'=`x'*`y'
}
}

*********************************
** Add FE's
*********************************

* Adding these FE's are the result of a diagnostic test done in FiveYrUJT

* add the fixed effects
gen labelclean3=labelclean
foreach x in oct2007lab_6 oct2009lab_24 apr2008lab_2 {
replace labelclean3="`x'" if `x'==1
}
encode labelclean3, gen(labelfac613)


****************************************
**  RSK score
****************************************
global ujt oct2007lab_1 oct2007lab_2 oct2007lab_3 oct2007lab_4 oct2007lab_5 oct2007lab_6 oct2007lab_7 oct2007lab_8 oct2007lab_9 oct2007lab_10 oct2007lab_11 oct2007lab_12 oct2007lab_13 oct2007lab_14 oct2007lab_15 oct2007lab_16 oct2007lab_17 oct2007lab_18 oct2007lab_19 oct2007lab_20 oct2007lab_21 oct2007lab_22 oct2007lab_23 oct2007lab_24 oct2007lab_25 oct2007lab_26 oct2007lab_27 oct2007lab_28 oct2007lab_29 oct2007lab_30 oct2007lab_31 oct2007lab_32 oct2007lab_33 apr2008lab_1 apr2008lab_2 apr2008lab_3 apr2008lab_4 apr2008lab_5 apr2008lab_6 apr2008lab_7 apr2008lab_8 apr2008lab_9 apr2008lab_10 apr2008lab_11 apr2008lab_12 apr2008lab_13 apr2008lab_14 apr2008lab_15 apr2008lab_16 apr2008lab_17 apr2008lab_18 apr2008lab_19 apr2008lab_20 apr2008lab_21 apr2008lab_22 apr2008lab_23 apr2008lab_24 apr2008lab_25 apr2008lab_26 apr2008lab_27 apr2008lab_28 apr2008lab_29 apr2008lab_30 apr2008lab_31 apr2008lab_32 apr2008lab_33 oct2008lab_1 oct2008lab_2 oct2008lab_3 oct2008lab_4 oct2008lab_5 oct2008lab_6 oct2008lab_7 oct2008lab_8 oct2008lab_9 oct2008lab_10 oct2008lab_11 oct2008lab_12 oct2008lab_13 oct2008lab_14 oct2008lab_15 oct2008lab_16 oct2008lab_17 oct2008lab_18 oct2008lab_19 oct2008lab_20 oct2008lab_21 oct2008lab_22 oct2008lab_23 oct2008lab_24 oct2008lab_25 oct2008lab_26 oct2008lab_27 oct2008lab_28 oct2008lab_29 oct2008lab_30 oct2008lab_31 oct2008lab_32 oct2008lab_33 apr2009lab_1 apr2009lab_2 apr2009lab_3 apr2009lab_4 apr2009lab_5 apr2009lab_6 apr2009lab_7 apr2009lab_8 apr2009lab_9 apr2009lab_10 apr2009lab_11 apr2009lab_12 apr2009lab_13 apr2009lab_14 apr2009lab_15 apr2009lab_16 apr2009lab_17 apr2009lab_18 apr2009lab_19 apr2009lab_20 apr2009lab_21 apr2009lab_22 apr2009lab_23 apr2009lab_24 apr2009lab_25 apr2009lab_26 apr2009lab_27 apr2009lab_28 apr2009lab_29 apr2009lab_30 apr2009lab_31 apr2009lab_32 apr2009lab_33 oct2009lab_1 oct2009lab_2 oct2009lab_3 oct2009lab_4 oct2009lab_5 oct2009lab_6 oct2009lab_7 oct2009lab_8 oct2009lab_9 oct2009lab_10 oct2009lab_11 oct2009lab_12 oct2009lab_13 oct2009lab_14 oct2009lab_15 oct2009lab_16 oct2009lab_17 oct2009lab_18 oct2009lab_19 oct2009lab_20 oct2009lab_21 oct2009lab_22 oct2009lab_23 oct2009lab_24 oct2009lab_25 oct2009lab_26 oct2009lab_27 oct2009lab_28 oct2009lab_29 oct2009lab_30 oct2009lab_31 oct2009lab_32 oct2009lab_33 apr2010lab_1 apr2010lab_2 apr2010lab_3 apr2010lab_4 apr2010lab_5 apr2010lab_6 apr2010lab_7 apr2010lab_8 apr2010lab_9 apr2010lab_10 apr2010lab_11 apr2010lab_12 apr2010lab_13 apr2010lab_14 apr2010lab_15 apr2010lab_16 apr2010lab_17 apr2010lab_18 apr2010lab_19 apr2010lab_20 apr2010lab_21 apr2010lab_22 apr2010lab_23 apr2010lab_24 apr2010lab_25 apr2010lab_26 apr2010lab_27 apr2010lab_28 apr2010lab_29 apr2010lab_30 apr2010lab_31 apr2010lab_32 apr2010lab_33 oct2010lab_1 oct2010lab_2 oct2010lab_3 oct2010lab_4 oct2010lab_5 oct2010lab_6 oct2010lab_7 oct2010lab_8 oct2010lab_9 oct2010lab_10 oct2010lab_11 oct2010lab_12 oct2010lab_13 oct2010lab_14 oct2010lab_15 oct2010lab_16 oct2010lab_17 oct2010lab_18 oct2010lab_19 oct2010lab_20 oct2010lab_21 oct2010lab_22 oct2010lab_23 oct2010lab_24 oct2010lab_25 oct2010lab_26 oct2010lab_27 oct2010lab_28 oct2010lab_29 oct2010lab_30 oct2010lab_31 oct2010lab_32 oct2010lab_33

*** run the test
qui reg rskscr   i.quarter i.labelfac613 c.rec_out#i.labelfac61 $ujt $supin & freqlabel>50
test $ujt

/*        F(146,  5394) =    1.13
            Prob > F =    0.1330
*/


* look at the fixed effects
sort numzz
matrix b_labtt=e(b)
matrix V_labtt=vecdiag(e(V))
matrix V_labtt = V_labtt'
matrix list V_labtt
matrix b_labtt=b_labtt'
matrix list b_labtt
svmat b_labtt
svmat V_labtt
replace b_labtt=. in 1/71
replace b_labtt=. in 303
gen se_labtt=sqrt(V_labtt)
gen tstat_rsktt=b_labtt/se_labtt
la var tstat_rsktt "T-statistics on linear time trend interacted with facility fixed effects - risk" 
gen absTrsktt=abs(tstat_rsktt)
gen prsktt=ttail(10000, absTrsktt)*2
sort prsktt
gen neg=-prsktt
sort neg
gen m=_n-1
sort prsktt
*put actual number of observations here
gen BHrsktt=prsktt*146/(146-m)
sort BHrsktt
*smallest FDR adjusted p value = .463
 drop b_labtt1 b_labtt1 V_labtt1 se_labtt neg m


* shift distribution to center
sum tstat_rsktt,d
gen tstat_rsk_c=tstat_rsktt-r(mean)
gen abs_trsk=-abs(tstat_rsk_c)
sort abs_trsk



****************************************
**  CH score
****************************************


**** run the test
qui reg chscr  i.quarter i.labelfac613 c.rec_out#i.labelfac61 $ujt $supin & freqlabel>50
test $ujt

/*       F(146,  5394) =    1.15
            Prob > F =    0.1082
            */


** look at the fixed effects
sort numzz
matrix b_labtt=e(b)
matrix V_labtt=vecdiag(e(V))
matrix V_labtt = V_labtt'
matrix list V_labtt
matrix b_labtt=b_labtt'
matrix list b_labtt
svmat b_labtt
svmat V_labtt
replace b_labtt=. in 1/71
replace b_labtt=. in 303
gen se_labtt=sqrt(V_labtt)
gen tstat_chtt=b_labtt/se_labtt
la var tstat_chtt "T-statistics on linear time trend interacted with facility fixed effects - risk" 
gen absTchtt=abs(tstat_chtt)
gen pchtt=ttail(10000, absTchtt)*2
sort pchtt
gen neg=-pchtt
sort neg
gen m=_n-1
sort pchtt
gen BHchtt=pchtt*146/(146-m)
sort BHchtt
 drop b_labtt1 b_labtt1 V_labtt1 se_labtt neg m
 
* shift distribution to center
sum tstat_chtt,d
gen tstat_ch_c=tstat_chtt-r(mean)
gen abs_tch=-abs(tstat_ch_c)
sort abs_tch 
 

drop $ujt 
drop apr2007 oct2007 apr2008 oct2008 apr2009 oct2009 apr2010 oct2010 lab_*
drop   tstat_chtt absTchtt pchtt BHchtt tstat_ch_c tstat_rsktt absTrsktt prsktt BHrsktt tstat_rsk_c     
